//
//  CZHomeViewController.swift
//  01-sinaWeibo-05-09
//
//  Created by mac on 16/5/9.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class CZHomeViewController: CZBaseTableViewController {
    
    //MARK :-属性
     private let reuseIdentifier = "cell"
    
    
    //要显示微博的数据
    
    var statuses:[CZStatus]? {
        
        didSet{
            self.tableView.reloadData()
            
        }
    
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        if !CZUserAccountViewModel.sharedInstance.isUserLogin{
        
            return
        }
        setupNavigationBar()
        
        //注册cell
//        tableView.registerClass(
//            UITableViewCell.self, forHeaderFooterViewReuseIdentifier: reuseIdentifier)
        
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
        CZStatusViewModel.sharedInstance.loadStaus {(statuses,error)->()in
            
            if error != nil{
            
                CZPrint(items: "请求微博数据失败了: \(error)")
                return

            }
            // 请求成功了
            CZPrint(items: "在控制器里面打印获取到的微博数据: \(statuses)")
            
            // 将网络请求返回的微博模型赋值给控制器的属性
            self.statuses = statuses
            return

        }
        
}
    
    private func setupNavigationBar(){
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", target: self, action: "didClickFriendsearch")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: "didClickPop")
        
        //获取用户名称
        let name = CZUserAccountViewModel.sharedInstance.userAccount?.screen_name ?? "未知名称"
        
        let button = CZHomeTitleButton()
        
        button.setTitle(name, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(16)
        button.setImage(UIImage(named:"navigationbar_arrow_down"), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "didClickTitlView:", forControlEvents: UIControlEvents.TouchUpInside)
        button.sizeToFit()
        navigationItem.titleView = button
        
    }
    //MARK:--点击事件
    
    func didClickTitlView(button: UIButton){
    
        // 三角形旋转依靠button.selected这个属性来确定
        // 1.修改按钮的状态
        button.selected = !button.selected
        
        //true false
        UIView.animateWithDuration(0.25) { () -> Void in
            let transfrom = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI-0.001)): CGAffineTransformIdentity
            button.imageView?.transform = transfrom
        }
        
    }
    
    func didClickFriendsearch(){
        
        CZPrint(items: "点击了左边")
       
    }
    
    func didClickPop(){
        CZPrint(items: "点击了右边")
        
        
    }
}


//扩展
extension CZHomeViewController {

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //根据identifier 去缓存池中查找 缓存池中有就返回一个cell 缓存池中没有就使用注册的cell类型来创建cell
        let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath)
        
        //获取对应的模型
        let status = self.statuses![indexPath.row]
        
        //设置cell内容
        cell.textLabel?.text = status.text
        
        return cell

    }
}






